#include "KeywordRanker.h"
#include <vector>
#include <iostream>
#include <math.h>
#include <fstream>

using namespace std;

KeywordRanker::KeywordRanker(vector<double> values)
:alphaValues(values)
{
}

KeywordRanker::~KeywordRanker()
{
}


vector<double> KeywordRanker::getMultiwordRank(const vector<vector<pair<uint, uint> > > & myVector) {
	uint numWords = myVector.size();
	uint numIds = myVector[0].size();
	vector<double> rankings(numIds);
	
//	ofstream file ("generico");
	
//	vector<vector<pair<uint,uint> > >::const_iterator it;
	double alphaSquareSum = 0;
	if (numWords != alphaValues.size()){
		cout << "Incoerencia entre numero de idfs e palavras no KeywordRanker" << endl;
	}

	for (uint j = 0; j < numWords; j++) {
		alphaSquareSum += alphaValues[j]*alphaValues[j];
	}
	for (uint i = 0; i < numIds; i++) {
		rankings[i] = 0;
		for (uint j = 0; j < numWords; j++) {
//			file << "j, i: " << j << ", " << i << "\n1, 2: " << myVector[j][i].first << ", " << myVector[j][i].second << endl;
			rankings[i] += alphaValues[j] * sqrt( double(myVector[j][i].second) );
		}
		rankings[i] = rankings[i] / sqrt(alphaSquareSum);
//		if (rankings[i] != 0)
//			file << "rankings[" << i << "]: " << rankings[i] << endl;
	}
//	file << "squaresum: " << sqrt(alphaSquareSum);
	return rankings;
}

vector<uint> KeywordRanker::getSingleWordRank(const vector<pair<uint, uint> > & myVector, vector<uint> & rankings) {
	uint numIds = myVector.size();
	if (rankings.size() != numIds) {
		cout << "Incoerencia no singleWordRank" << endl;
		if (rankings.size() < numIds)
			numIds = rankings.size();
	}
	for (uint i = 0; i < numIds; i++) 
		rankings[i] += uint( weight*sqrt( double(myVector[i].second) ) );
	return rankings;
}
